﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Asset
{
    public class TestLoad : MonoBehaviour
    {
        public GameObject loadingBg;
        public Image loadingImg;
        public Text loadingText;
        [Space(20)]
        public Image image;
        public Image image2;
        public Image image3;

        private void Start()
        {
            GameObject.DontDestroyOnLoad(this);
            PoolMgr.Instance.init();
            //loadingBg = GameObject.Find("loadingImg");
            //loadingImg = loadingBg.transform.Find("loadingFillImg").GetComponent<Image>();
            //loadingText = loadingBg.transform.Find("loadingFillText").GetComponent<Text>();
            loadingBg.SetActive(false);
        }

        string name1 = "100100001";
        string name2 = "bossbg";
        string name3 = "100100003";

        private void OnGUI()
        {
            if (GUILayout.Button("加载1", GUILayout.Width(200), GUILayout.Height(45)))
            {
                //异步
                PoolMgr.loadAssetAsync<Sprite>(name1).onComplete += loadFinish1;
                //中止
                PoolMgr.loadAssetAsync<Sprite>(name1).onComplete -= loadFinish1;
                //同步
                this.image.sprite = PoolMgr.loadAsset<Sprite>(name1);
                //ResPool.Instance.loadAssetAsync<Sprite>(url2, name2).onComplete += loadFinish1;
                PoolMgr.loadAssetAsync<Sprite>(name2).onComplete += loadFinish2;
                PoolMgr.loadAssetAsync<Sprite>(name3).onComplete += loadFinish3;
            }

            if (GUILayout.Button("加载2", GUILayout.Width(200), GUILayout.Height(45)))
            {
                string name3 = "bossbg";
                PoolMgr.loadAssetAsync<Sprite>(name3).onComplete += loadFinish3;
            }
            if (GUILayout.Button("加载skillui", GUILayout.Width(200), GUILayout.Height(45)))
            {
                string uiName = "skillui";
                //异步加载
                PoolMgr.instantiateObjAsync(uiName, loadSkillUIFinish);
            }
            if (GUILayout.Button("设置skillui Tex", GUILayout.Width(200), GUILayout.Height(45)))
            {
                if (this.skillui != null)
                {
                    string texName = "Tblue";
                    //同步
                    //this.skillui.transform.GetChild(1).GetComponent<Image>().sprite = PoolMgr.loadAsset<Sprite>(texName);
                    //this.skillui.GetComponent<PoolObj>().addDymicRefByAsset(texName);
                    //异步
                    PoolMgr.loadAssetAsync<Sprite>(texName).onComplete += setSkillTex;
                    this.skillui.GetComponent<PoolObj>().addDymicRefByAsset(texName);
                }
                else
                {
                    Debug.LogError("ui还没有加载出来");
                }
            }
            if (GUILayout.Button("卸载skillui", GUILayout.Width(200), GUILayout.Height(45)))
            {
                //回收
                //PoolMgr.recyleObj(skillui);
                //销毁
                GameObject.Destroy(skillui);
            }

            if (GUILayout.Button("实例化100 cube", GUILayout.Width(200), GUILayout.Height(45)))
            {
                string uiName = "cube";
                for (int i = 0; i < 1000; i++)
                {
                    PoolMgr.instantiateObjAsync(uiName, onLoadCubeFinish);
                }
            }

            if (GUILayout.Button("加载场景level_01", GUILayout.Width(200), GUILayout.Height(45)))
            {
                string uiName = "cube";
                for (int i = 0; i < 1000; i++)
                {
                    PoolMgr.unloadObj(uiName, onLoadCubeFinish);
                }
                loadingBg.SetActive(true);
                List<PreLoadInfo> lst = new List<PreLoadInfo>();
                lst.Add(new PreLoadInfo("cube", 10));
                PoolMgr.loadSceneAsync("level_01", onLoadingScene, null);
            }

            if (GUILayout.Button("加载场景level_02", GUILayout.Width(200), GUILayout.Height(45)))
            {
                loadingBg.SetActive(true);
                List<PreLoadInfo> lst = new List<PreLoadInfo>();
                lst.Add(new PreLoadInfo("skillui", 3));
                PoolMgr.loadSceneAsync("level_02", onLoadingScene, lst);
            }
        }

        void onLoadingScene(int progress, string sceneName)
        {
            if (progress >= 100)
            {
                loadingBg.SetActive(false);
            }
            else
            {
                loadingImg.fillAmount = progress / 100;
                loadingText.text = progress + "%";
            }
        }

        void setSkillTex(Sprite sp)
        {
            this.skillui.transform.GetChild(1).GetComponent<Image>().sprite = sp;
        }

        int index = 0;

        void onLoadCubeFinish(GameObject go)
        {
            index++;
            int row = Mathf.FloorToInt(index / 10);
            int col = index % 10;
            Vector3 pos = new Vector3(col * 2, 0, row * 2);
            go.transform.position = pos;
        }

        GameObject skillui;
        void loadSkillUIFinish(GameObject go)
        {
            go.transform.SetParent(this.transform);
            go.transform.localPosition = Vector3.zero;
            skillui = go;
        }

        void loadFinish1(Sprite sp)
        {
            image.sprite = sp;
        }
        void loadFinish2(Sprite sp)
        {
            image2.sprite = sp;
        }
        void loadFinish3(Sprite sp)
        {
            image3.sprite = sp;
        }


    }
}
